# -*- coding:utf-8 -*-
"""
@Time : 2021/1/11 17:22
@Author: langengel
@Des: 基本模型
"""
from tortoise import Model, fields


class User(Model):
    username = fields.CharField(null=True, max_length=20, description="用户名")
    type = fields.BooleanField(default=False, description="用户类型 True:超级管理员 False:普通管理员")
    password = fields.CharField(null=True, max_length=255)
    nickname = fields.CharField(default='阑羹阁兒', max_length=255, description='昵称')
    u_phone = fields.CharField(null=True, description="手机号", max_length=11)
    u_email = fields.CharField(null=True, description='邮箱', max_length=255)
    full_name = fields.CharField(null=True, description='姓名', max_length=255)
    u_status = fields.IntField(default=0, description='0未激活 1正常 2禁用')
    headimgurl = fields.CharField(null=True, max_length=255, description='头像')
    sex = fields.IntField(default=0, null=True, description='0未知 1男 2女')
    remarks = fields.CharField(null=True, max_length=30, description="备注")
    client_host = fields.CharField(null=True, max_length=19, description="访问IP")
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    roles = fields.ManyToManyField("sups.Role", related_name="user_roles", on_delete=fields.CASCADE)

    class Meta:
        table_description = "用户"
        table = "user"


class Role(Model):
    role_name = fields.CharField(max_length=15, description="角色名称")
    rules = fields.ManyToManyField("sups.Rule", related_name="role_rule", on_delete=fields.CASCADE)
    role_status = fields.BooleanField(default=True, description="True:启用 False:禁用")
    role_desc = fields.CharField(null=True, max_length=255, description='角色描述')
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table_description = "角色"
        table = "role"


class Rule(Model):
    rule_name = fields.CharField(max_length=15, description="权限名称")
    parent_id = fields.IntField(default=0, description='父id')
    scopes = fields.CharField(unique=True, max_length=255, description='权限范围标识')
    rule_desc = fields.CharField(null=True, max_length=255, description='权限描述')
    is_check = fields.IntField(default=1, description='权限状态 1权限 2必选权限')
    is_menu = fields.IntField(default=2, description='是否为菜单 1菜单 2不是菜单')
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table_description = "系统权限"
        table = "rule"


class Wechat(Model):
    city = fields.CharField(null=True, max_length=255, description='城市')
    country = fields.CharField(null=True, max_length=255, description='国家')
    headimgurl = fields.CharField(null=True, max_length=255, description='微信头像')
    nickname = fields.CharField(null=True, max_length=255, description='微信昵称')
    openid = fields.CharField(null=True, max_length=255, description='openid')
    province = fields.CharField(null=True, max_length=255, description='省份')
    sex = fields.IntField(null=True, description='性别')
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    user = fields.ForeignKeyField("sups.User", related_name="wechat_user", on_delete=fields.CASCADE)

    class Meta:
        table_description = "用户微信"
        table = "wechat"
